/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.service.performance;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.bean.PropValueBean;
import com.sinodata.bsm.center.cache.PropertyValueCache;
import com.sinodata.bsm.center.dao.PropertyValueDao;
import com.sinodata.bsm.common.vo.PropertyValue;

/**
 * <p>
 * Description: 指标查询的服务类 注： 凡是需要分页的都按时间倒序 凡是不需要分页的都按时间正序
 * </p>
 * 
 * @author wangyazhou
 * @version 1.0
 * 
 * <p>
 * History:
 * 
 * Date Author Version Description
 * ---------------------------------------------------------------------------------
 * Jul 5, 2012 2:23:00 PM wangyazhou 1.0 To create
 * </p>
 * 
 * @since
 * @see
 */

@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class PropValueService {

    @Autowired
    private PropertyValueCache propertyValueCache;

    @Autowired
    private PropertyValueDao propValueDao;

    /**
     * 获取一个资源的一个指标值 从缓存中获取
     * 
     * @param resId
     *            资源ID
     * @param propId
     *            指标ID
     * @return
     */
    @SuppressWarnings("deprecation")
    public PropValueBean getPropValue(Long resId, Long propId) {
        String key = resId + "&" + propId;
        return propertyValueCache.get(key);
    }

    /**
     * 获取一个资源一段时间内的指标值 无分页 可用来画图 、导出
     * 
     * @param resId
     *            资源ID
     * @param storeOrder
     *            指标的存入位置
     * @param startDate
     *            开始时间
     * @param endDate
     *            结束时间
     * @param dimen
     *            0:原始指标 1:时汇聚 2:日汇聚
     * @return
     */
    public List<PropertyValue> getPropValues(Long resId, int[] storeOrder, String startDate, String endDate, String dimen) {
        List<PropertyValue> propValueList = new ArrayList<PropertyValue>();
        if ("0".equals(dimen)) {
            propValueList = propValueDao.getPropValuesByDate(resId, storeOrder, startDate, endDate);
        } else {
            propValueList = propValueDao.getReducePropByDate(resId, storeOrder, startDate, endDate, dimen);
        }
        return propValueList;
    }

    public List<PropertyValue> getThrendComparePropvalus(long resId, int[] storeOrder, String startDate, String endDate, String trendType, String dimen, String choiceType) {
        List<PropertyValue> propValueList = new ArrayList<PropertyValue>();
        propValueList = propValueDao.getThendPropCompareByDate(resId, storeOrder, startDate, endDate, trendType, dimen, choiceType);
        return propValueList;

    }

    /**
     * 从汇聚表获取时、日的指标汇聚值 无分页 可用来绘图、导出
     * 
     * @param 资源ID
     * @param storeOrder
     *            指标存放位置
     * @param startDate
     *            开始日期
     * @param endDate
     *            结束日期
     * @param rtrTypSrdimeningeendType
     *            趋势类型 0:均值 1:波峰 2:波谷
     * @param dimen
     *            数据维度 1:时汇聚 2:日汇聚
     * @return
     */
    public List<PropertyValue> getTrendPropValus(Long resId, int[] storeOrder, String startDate, String endDate, String trendType, String dimen) {
        List<PropertyValue> propValueList = new ArrayList<PropertyValue>();
        propValueList = propValueDao.getTrendPropByDate(resId, storeOrder, startDate, endDate, trendType, dimen);
        return propValueList;
    }

    /**
     * 获取分页的指标数据 指标查询时用
     * 
     * @param resId
     *            资源ID
     * @param page
     *            页
     * @param storeOrder
     *            指标的存放位置
     * @param startDate
     *            开始日期
     * @param endDate
     *            结束日期
     * @param dimen
     *            数据维度 0:原始 1:时汇聚 2:日汇聚
     * @return
     */
    @SuppressWarnings("rawtypes")
    public Page getPropValuesByPage(Long resId, Page page, int[] storeOrder, String startDate, String endDate, String dimen) {
        if ("0".equals(dimen)) {
            page = propValueDao.getPropValuesByPage(resId, page, storeOrder, startDate, endDate);
        } else {
            page = propValueDao.getReducePropValuesByPage(resId, page, storeOrder, startDate, endDate, dimen);
        }
        return page;
    }

    /**
     * 获取某个资源下的指标数(不分页)
     * 
     * @param resId
     * @param storeOrder
     * @param startDate
     * @param endDate
     * @param dimen
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<PropertyValue> getPropValuesList(Long resId, int[] storeOrder, String startDate, String endDate, String dimen) {
        return propValueDao.getPropValuesByDate(resId, storeOrder, startDate, endDate);
    }

    /**
     * 根据区间段查询分布
     * 
     * @param resId
     * @param storeOrder
     * @param startDate
     * @param endDate
     * @param dimen
     * @param spaceTimeList
     * @return
     */
    public Map<String, String> getCountPropValuesBySpace(Long resId, int[] storeOrder, String startDate, String endDate, String dimen, List<String> spaceTimeList) {
        return propValueDao.queryCountPropValuesBySpace(resId, storeOrder, startDate, endDate, dimen, spaceTimeList);
    }

    public String queryMaxPropValueByDate(Long resId, int[] storeOrder, String startDate, String endDate) {
        return propValueDao.queryMaxPropValueByDate(resId, storeOrder, startDate, endDate);

    }

    /**
     * 计算 指标的公差
     * 
     * @param resId
     * @param storeOrder
     * @param startDate
     * @param endDate
     * @param dimen
     * @return
     */

    public List<PropertyValue> getPropValValueList(Long resId, int[] storeOrder, String startDate, String endDate, String dimen) {
        return propValueDao.getPropValValueByDate(resId, storeOrder, startDate, endDate, dimen);

    }

    /**
     * 获取大字段的指标值
     * 
     * @param resId
     *            资源ID
     * @param page
     *            页
     * @param storeOrder
     *            存储位置
     * @param startDate
     *            开始日期
     * @param endDate
     *            结束日期
     * @return
     */
    @SuppressWarnings("rawtypes")
    public Page getLogContentProps(Long resId, Page page, int[] storeOrder, String startDate, String endDate) {
        return propValueDao.getLogContentPropsByPage(resId, page, storeOrder, startDate, endDate);
    }

    /**
     * 执行 "检查指标是否成功采集" 的存储过程
     * 
     * @return
     */
    public String doPropCheckProc() {
        return propValueDao.doPropCheckProc();
    }

    /**
     * 获取执行指标检查的时间
     * 
     * @return
     */
    public String queryCheckedTime() {
        return propValueDao.queryCheckedTime();
    }

    /**
     * 查询 "指标采集验证后的记录"
     * 
     * @param page
     * @param condition
     * @return
     */
    public Page<String[]> queryCheckedProp(Page<String[]> page, Condition condition) {

        return propValueDao.queryCheckedProp(page, condition);
    }

}
